package com.cryptape.cita_wallet.activity.cloudminer;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.cryptape.cita_wallet.CytonApplication;
import com.cryptape.cita_wallet.activity.MangoNodeActivity;
import com.cryptape.cita_wallet.activity.MinerPayActivity;
import com.cryptape.cita_wallet.activity.NBaseActivity;
import com.cryptape.cita_wallet.activity.transfer.TransferActivity;
import com.cryptape.cita_wallet.adapter.MyMinerAdapter;
import com.cryptape.cita_wallet.bean.LightNodeBean;
import com.cryptape.cita_wallet.bean.MyHMIOOrderBean;
import com.cryptape.cita_wallet.bean.MyHMIOOrderItem;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.item.Token;
import com.cryptape.cita_wallet.net.callback.CommonCallback;
import com.cryptape.cita_wallet.net.request.HttpRequestMode;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.util.RSAUtils;
import com.cryptape.cita_wallet.util.db.DBWalletUtil;
import com.cryptape.cita_wallet.view.SpacesItemDecoration;
import com.cryptape.cita_wallet.view.TitleBar;
import com.mangoworld.android.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

/**
 * Created by dengming on 26/2/20.
 */

public class MyMinerActivity extends NBaseActivity {

    private RecyclerView recyclerView;
    private MyMinerAdapter minerAdapter;
    private TitleBar mTitle;
    private List<MyHMIOOrderItem> myMinerDatas = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_my_miner;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.my_miner_recycler);
        mTitle = findViewById(R.id.title_bar);
        mTitle.setOnRightClickListener(new TitleBar.OnRightClickListener() {
            @Override
            public void onRightClick() {
                startActivity(new Intent(MyMinerActivity.this, MinerIncomeActivity.class));
            }
        });
        mTitle.mTitleBarRightDefaultView.setCompoundDrawables(null, null, null, null);
    }

    @Override
    protected void initData() {
        initMinerData();
    }

    @Override
    protected void initAction() {

    }

    private void initMinerData() {
        initAdapter();
        try {
            String address = DBWalletUtil.getCurrentWallet(CytonApplication.getInstance()).address;
            Map params = MapUtils.newHashMap();
            params.put("address", address);
            String jsonData = GsonUtils.toJson(params);
            String content = RSAUtils.encrypt(jsonData);
            HttpRequestMode.getMyHMioOrders(MyMinerActivity.this, content, true, new CommonCallback() {
                @Override
                public void onSuccess(Object data, String successMsg, int responseSuccessCode) {
                    if (data != null) {
                        MyHMIOOrderBean myHMIOOrderBean = (MyHMIOOrderBean) data;
                        myMinerDatas.clear();
                        if (myHMIOOrderBean.getList() != null) {
                            myMinerDatas.addAll(myHMIOOrderBean.getList());
                        }
                        minerAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onFailure(String msg, int responseFailCode) {
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(10));
//        datas.add(new CloudMinerBean());
//        MyHMIOOrderItem item = new MyHMIOOrderItem();
//        item.setCalPower("1T");
//        item.setLevelName("M1");
//        item.setCreateTime("2019-1");
//        item.setStatus(1);
//        item.setStatusName("挖矿中");
//        item.setSurplusMoney("3000");
//        myMinerDatas.add(item);
        minerAdapter = new MyMinerAdapter(myMinerDatas);
        recyclerView.setAdapter(minerAdapter);

        minerAdapter.setOnItemClickListener((view, position) -> {
            Token token = new Token(ConstantUtil.ETH_MAINNET, ConstantUtil.HMIO_TOKEN_SYMBOL, ConstantUtil.ETHEREUM_MAIN_ID);
            Intent intent = new Intent(MyMinerActivity.this, MinerPayActivity.class);
            token.setContractAddress(ConstantUtil.getHToken());// "0xb2054aDd212F19581aEB195FDa7644471a996134";
            token.decimals = 18;
            intent.putExtra(TransferActivity.EXTRA_TOKEN, token);
            intent.putExtra(TransferActivity.EXTRA_ADDRESS, ConstantUtil.getMinerSellContract());
            startActivity(intent);
        });
    }
}